package ecommerce.user.behavior.analysis.test;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;

/**
 * 描述: 生成密钥
 *
 * @author K·Herbert
 * @since 2025-02-16 下午8:56
 */
public class GenKey {
    // AES加密算法
    private static final String ALGORITHM = "AES";

    // 生成一个随机的AES密钥（128位）
    public static String generateKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
        keyGen.init(128); // 128位密钥
        SecretKey secretKey = keyGen.generateKey();
        return Base64.getEncoder().encodeToString(secretKey.getEncoded());
    }

    public static void main(String[] args) throws Exception {
        System.out.println("[" + GenKey.generateKey() + "]");
    }
}
